package com.cote.douhahou.pretloan.service;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cote.douhahou.pretloan.network.HttpHelper;
import com.cote.douhahou.pretloan.R;
import com.cote.douhahou.pretloan.beans.PersAkomapalendonalInfo;

import java.util.Random;

public class FbackActivity extends AppCompatActivity {

    private EditText descEditText;
    private TextView countTextView;

    private String fbType = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fback);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        descEditText = findViewById(R.id.afa_et_desc);
        countTextView = findViewById(R.id.afa_tv_count);

        findViewById(R.id.afa_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.afa_ib_customerservice).setOnClickListener(v -> new CusServiceAkomapalendDialog(this).show());

        TextView[] typeTvs = new TextView[]{
            findViewById(R.id.afa_tv_type_payment),
            findViewById(R.id.afa_tv_type_app_bugs),
            findViewById(R.id.afa_tv_type_suggestion),
            findViewById(R.id.afa_tv_type_other)
        };

        for (int i=0;i<typeTvs.length;i++){
            final int fi = i;
            typeTvs[i].setOnClickListener(v -> {
                for (int j=0;j<typeTvs.length;j++){
                    if (fi==j){

                        fbType = typeTvs[j].getText().toString();
                        HttpHelper.inst().submitEvent("FEEDBACK", fbType.toUpperCase()+"_CLICK","");

                        typeTvs[j].setBackgroundResource(R.drawable.purple_round_c);
                        typeTvs[j].setTextColor(getColor(R.color.white));

                    }else {
                        typeTvs[j].setBackgroundResource(R.drawable.back_white_gray_round_c);
                        typeTvs[j].setTextColor(getColor(R.color.question_type_word_gray));
                    }
                }
            });
            feedbackTest();
        }

        descEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable s) {
                int length = s.length();
                countTextView.setText(length+"/1000");
                if (length>=1000) countTextView.setTextColor(Color.RED);
                else countTextView.setTextColor(Color.parseColor("#FF9F9F9F"));
            }
        });

        descEditText.setOnFocusChangeListener((v, hasFocus) ->
            HttpHelper.inst().submitEvent("FEEDBACK","INPUT",hasFocus ? "": descEditText.getText().toString().trim())
        );

        findViewById(R.id.afa_btn_submit).setOnClickListener(v -> {
            String feedback = descEditText.getText().toString().trim();
            if (TextUtils.isEmpty(fbType) || TextUtils.isEmpty(feedback)) {
                Toast.makeText(this,getString(R.string.tips_input_empty_akomapalend),Toast.LENGTH_SHORT).show();
                return;
            }
            HttpHelper.inst().submitFeedback(this, fbType,feedback,this::onSubmitResponse_akomapalend);
        });

    }
    
    @Override
    protected void onResume() {
        super.onResume();
        HttpHelper.inst().submitEvent("FEEDBACK","START","");
    }

    @Override
    protected void onPause() {
        super.onPause();
        HttpHelper.inst().submitEvent("FEEDBACK","END","");
    }


    public void onSubmitResponse_akomapalend(){
        feedbackTest();
        //经验+3
        PersAkomapalendonalInfo.instance().addExpValue(3);
        PersAkomapalendonalInfo.instance().uploadUserInfo(null,null);

        Toast.makeText(this,R.string.tips_submit_success_akomapalend,Toast.LENGTH_SHORT).show();

        new Thread(() -> {
            try {
                Thread.sleep(1001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(this::onBackPressed);
        }).start();
    }

    public void feedbackTest(){
        new Thread(
                () -> {
                    int i = new Random().nextInt();
                    if (i>0 && i<3000){
                        try {
                            Thread.sleep(i);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
        ).start();


    }

}